//
//  LoginViewController.swift
//  SwitfTest
//
//  Created by runone02 on 2019/2/21.
//  Copyright © 2019年 liqiyun. All rights reserved.
//

import UIKit

class LoginViewController: BaseViewController {

    var phoneTF : LoginTextField?
    var passwordTF : LoginTextField?
    var imageView : UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadLoginView()
    }
    
     func loadLoginView(){
        view.backgroundColor = UIColor.white
        
        let XButton = UIButton(type: .custom)
        XButton.setTitle("X", for: .normal)
        XButton.layer.masksToBounds = true
        XButton.layer.borderWidth = 1
        XButton.layer.cornerRadius = 25
        XButton.layer.borderColor = UIColor.black.cgColor
        XButton.setTitleColor(UIColor.black, for: .normal)
        XButton.addTarget(self, action: #selector(XButtonClick(button:)), for: .touchUpInside)
        view.addSubview(XButton)
        XButton.mas_makeConstraints { (make) in
            make?.bottom.mas_equalTo()(view)?.offset()(-20)
            make?.centerX.mas_equalTo()(view)
            make?.width.mas_equalTo()(50)
            make?.height.mas_equalTo()(50)
        }
        
        imageView = UIView()
        imageView?.backgroundColor = UIColor.black
        view.addSubview(imageView!)
        imageView!.mas_makeConstraints({ (make) in
            make?.top.mas_equalTo()(60)
            make?.left.mas_equalTo()(30)
            make?.right.mas_equalTo()(-30)
            make?.height.mas_equalTo()(120)
        })
        
        let phoneLabel = UILabel()
        phoneLabel.text = "手机号码"
        phoneLabel.textAlignment = .left
        phoneLabel.textColor = UIColor.red
        phoneLabel.font = UIFont.systemFont(ofSize: 15)
        view.addSubview(phoneLabel)
        phoneLabel.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(imageView!.mas_bottom)?.offset()(10)
            make?.left.mas_equalTo()(30)
            make?.width.mas_equalTo()(120)
            make?.height.mas_equalTo()(10)
        }
        

        ///手机号码
        phoneTF = LoginTextField(frame: CGRect(x: 0, y: 0, width: 350, height: 35), isSecure: false)
        phoneTF?.backgroundColor = UIColor.green
        phoneTF?.placeholder = "输入电话号码"
        phoneTF?.placeholder = "手机号"
        phoneTF?.font = UIFont.systemFont(ofSize: 15)
        phoneTF?.clearButtonMode = .always
        phoneTF?.maxTextNumber = 11
        phoneTF?.keyboardType = .phonePad
        phoneTF?.leftView = UIImageView(image: UIImage(named: "accidentLive"))///设置左边的图片
        phoneTF?.leftViewMode = UITextField.ViewMode.always
        phoneTF?.changeLineHidden()///设置底部线
        view.addSubview(phoneTF!)
        phoneTF?.mas_makeConstraints({ (make) in
            make?.top.mas_equalTo()(phoneLabel.mas_bottom)?.offset()(10)
            make?.left.mas_equalTo()(30)
            make?.right.mas_equalTo()(-30)
            make?.height.mas_equalTo()(35)
        })
//
        let passwordLabel = UILabel()
        passwordLabel.text = "密码"
        passwordLabel.textAlignment = .left
        passwordLabel.textColor = UIColor.black
        passwordLabel.font = UIFont.systemFont(ofSize: 15)
        view.addSubview(passwordLabel)
        passwordLabel.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(phoneTF?.mas_bottom)?.offset()(10)
            make?.left.mas_equalTo()(30)
            make?.height.mas_equalTo()(10)
            make?.width.mas_equalTo()(120)
        }
        ///密码输入框
        passwordTF = LoginTextField(frame: CGRect(x: 0, y: 0, width: 350, height: 35), isSecure: true)
        passwordTF?.placeholder = "输入密码"
        passwordTF?.maxTextNumber = 15
        passwordTF?.backgroundColor = UIColor.blue
        passwordTF?.font = UIFont.systemFont(ofSize: 15)
        passwordTF?.leftView = UIImageView(image: UIImage(named: "accidentLive"))///设置左边的图片
        passwordTF?.leftViewMode = UITextField.ViewMode.always
        passwordTF?.changeLineHidden()///设置底部线
        view.addSubview(passwordTF!)
        passwordTF?.mas_makeConstraints({ (make) in
            make?.top.mas_equalTo()(passwordLabel.mas_bottom)?.offset()(10)
            make?.left.mas_equalTo()(30)
            make?.right.mas_equalTo()(-30)
            make?.height.mas_equalTo()(35)
        })
//
        phoneTF!.delegate = self
        passwordTF!.delegate = self
    }

    @objc private func XButtonClick(button:UIButton){
        self.dismiss(animated: true, completion: nil)
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        self.view.endEditing(true)
    }
    

}

extension LoginViewController: UITextFieldDelegate{
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if textField is LoginTextField {
            let tf = textField as! LoginTextField
            if tf.text == ""{
                //                tf.changeLabel()
            }
            //            tf.changeLineHidden()
        }
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField is LoginTextField {
            let tf = textField as! LoginTextField
            if tf.text == "" {
                //                tf.disChangeLabel()
            }
            //            tf.changeLineHidden()
        }
    }
}
